/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class InverterNode extends WiringNode implements WiringConsumer, WiringOutput {

    private boolean inputSet = false;
    private float value = 0;
    private int type;
    private int consumerIndex;
    private int axis = 0;
    private Vector<WiringConsumer> consumers = new Vector();
    private float outVal = 0;
    Vector<ConnectionInfo> connections = new Vector();

    public void setConsumer(WiringConsumer consumer) {
        consumers.add(consumer);
    }

    public void invalidate() {

        finished = false;
        inputSet = false;
    }

    public InverterNode(int x, int y, TrueTypeFont font, Vector<Selectable> selectSet) {
        this.selectSet = selectSet;
        this.font = font;
        this.locX = x;
        this.locY = y;

    }

    public void evaluate() {
        //don't do anything until our parent node has set our value
        //if no output, then don't wait for this node
        if (consumers.size() == 0) {
            finished = true;
        } else if (inputSet) {
            value = -value;
            for (WiringConsumer wc : consumers) {
                wc.setValue(value);
            }
            finished = true;
        }

    }

    public void lineToConsumer() {

        for (WiringConsumer wc : consumers) {
            PVector center = wc.getScreenCenter();

            GL11.glColor3f(0, 0, 1);

            GL11.glPushMatrix();

            GL11.glBegin(GL11.GL_LINES);
            GL11.glVertex3f(locX + cWidth / 2, locY, 0);

            //connect nodes at the edges. For objects on the planes, go right to the center
            if (wc instanceof WiringNode) {
                GL11.glVertex3f(center.x - cWidth / 2, center.y, 0);
            } else {
                GL11.glVertex3f(center.x, center.y, 0);
            }
            GL11.glEnd();
            GL11.glPopMatrix();
        }

    }

    public void draw() {
        super.draw();
        lineToConsumer();
        GL11.glColor4f(parent.TEXT[0], parent.TEXT[1], parent.TEXT[2], parent.TEXT[3]);
        //parent.font.drawString(locX + 5, locY, title, 1, 1);
        font.drawString(locX + 5, locY, "Negative:" + value, 1f, 1f);

    }

    public PVector getCentroid() {
        return new PVector(locX, locY, 0);
    }

    public void hookUp(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {
        for (ConnectionInfo ci : connections) {
            System.out.println("adding connection" + ci.type + ":" + ci.index);
            if (ci.index != -1) {
                if (ci.type == 0) {
                    setConsumer((WiringConsumer) wiring.elementAt(ci.index));
                } else if (ci.type == 1) {
                    setConsumer((WiringConsumer) nodes.elementAt(ci.index));
                } else if (ci.type == 2) {
                    setConsumer((WiringConsumer) constraints.elementAt(ci.index));
                }

            }
        }
    }

    public InverterNode(String[] line, TrueTypeFont font, Vector<Selectable> selectSet) {
        this(Integer.parseInt(line[1]),
                Integer.parseInt(line[2]),
                font, selectSet);


                for (int i = 3; i < line.length; i += 2) {
            connections.add(new ConnectionInfo(Integer.parseInt(line[i]),
                    Integer.parseInt(line[i + 1])));

        }


    }

    public String pickle(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        String ret = "I,"
                + locX + ","
                + locY + ",";

        for (WiringConsumer wc : consumers) {
            if (wc instanceof WiringNode) {
                ret += 0 + "," + wiring.indexOf(wc) + ",";
            } else if (wc instanceof Coupling || wc instanceof ControllerConstraint) {
                ret += 2 + "," + constraints.indexOf(wc) + ",";
            } else {
                ret += 1 + "," + nodes.indexOf(wc) + ",";
            }

        }
        return ret;
    }

    public void setValue(float value) {
        this.value = value;
        inputSet = true;
    }

    public PVector getScreenCenter() {
        return new PVector(locX, locY, 0);
    }
}
